import groovy.json.*
import org.apache.commons.lang.StringUtils
import redis.clients.jedis.*
import com.alibaba.moco.util.*
import com.alibaba.moco.*

/**
 * 模拟 滴滴 客户投诉
 * Created by wb-lj240895 on 2016/11/1.
 */
def excute(request) {
    def slurper = new JsonSlurper()
    def params = request.getParameterMap();
    Jedis redis = RedisPoolInstance.getRedisClient()
    //欢行异常判断
    def e = redis.hget("exception","complaint")
    if (!StringUtils.isBlank(e)) {
        def eJson = slurper.parseText(e)
        return eJson
    }
    ComplaintRequest req = BaseTypeUtil.mapToBean(params, ComplaintRequest.class) as ComplaintRequest
    String token = req.getAccess_token()
    System.out.println(token)
    ComplaintResponse response = new ComplaintResponse()
    if (token != null /* && redis.exists(token)*/) {
        //redis.del(token)
        //TODO:判断订单是否存在
        response.setErrno(0)
        response.setErrmsg("SUCCESS")
        return JsonOutput.toJson(response)
    } else {
        response.setErrno(-1)
        response.setErrmsg("token错误")
        return JsonOutput.toJson(response)
    }


}

public class ComplaintRequest {
    String client_id	//	string	yes	申请应用时分配的AppKey
    String access_token	//	string	yes	授权后的access token
    long order_id	//	bigint	yes	订单id
    int type	//	int	yes	投诉选项id(从投诉选项接口返回的结果中选取)
    String content	//	string	no	投诉选项外的其他投诉内容,不能多于40个汉字
    String timestamp	// int	yes	当前时间戳
    String sign	//	string	yes	签名
}

public class ComplaintResponse {
    int errno
    String errmsg
    String data
}
